home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / pid.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  1KB  |  47 lines

  1. /* --------------------------------- pid.c ---------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* Automatic control. The PID method calculates the system response (output)
  8.  * to an error signal (input) as the sum of three components which are termed
  9.  * Proportional, Integral and Derivative. It is used by the auto-pilot system.
  10. */
  11.  
  12. #include "fly.h"
  13.  
  14.  
  15. extern long FAR
  16. pid_calc (PID *pid, long P, int interval)
  17. {
  18.     long    D, s;
  19.  
  20.     if (P < pid->Iband && P > -pid->Iband)
  21.         pid->I += P*interval;
  22.     else
  23.         pid->I = 0;
  24.  
  25.     if (P < pid->Dband && P > -pid->Dband) {
  26.         D = P - pid->Pprev;
  27.         D = D*1000L/interval;
  28.     } else
  29.         D = 0;
  30.     pid->Pprev = P;
  31.  
  32.     s = (P*pid->Kp + pid->I/pid->Ki + D*pid->Kd)/pid->factor;
  33. #if 0
  34. if (pid == EE(CC)->PIDroll) {
  35.     STATS_TEMP2[0] = P;
  36.     STATS_TEMP2[1] = P*pid->Kp/pid->factor;
  37.     STATS_TEMP2[2] = pid->I/pid->Ki/pid->factor;
  38.     STATS_TEMP2[3] = D*pid->Kd/pid->factor;
  39. }
  40. #endif
  41.     if (s > pid->range)
  42.         s = pid->range;
  43.     else if (s < -pid->range)
  44.         s = -pid->range;
  45.     return (s);
  46. }
  47.